﻿using System;
using System.Linq.Expressions;
using System.Reflection;
using PowerfulExtensions.Helpers;

namespace PowerfulExtensions.Attributes
{
    public static class AttributeHelper
    {
        public static TAttribute GetAttribute<TAttribute>(this MemberInfo propertyInfo) where TAttribute : Attribute
        {
            return (TAttribute)propertyInfo.GetCustomAttributes(typeof(TAttribute), false)[0];
        }

        public static TAttribute GetAttribute<TAttribute>(Expression<Func<object>> exp) where TAttribute : Attribute
        {
            return GetAttribute<TAttribute>(ExpressionHelper.GetMember(exp));
        }

        public static TAttribute GetAttribute<TClass, TAttribute>(Expression<Func<TClass, object>> exp) where TAttribute : Attribute
        {
            return GetAttribute<TAttribute>(ExpressionHelper.GetMember(exp));
        }

        public static TAttribute GetAttribute<TClass, TAttribute>() where TAttribute : Attribute
        {
            return (typeof (TClass)).GetAttribute<TAttribute>();
        }
    }
}
